package com.ltt.service.impl;

import com.ltt.dao.pojo.FileItems;
import com.ltt.dao.pojo.User;
import com.ltt.mapper.ResourcesMapper;
import com.ltt.service.ResourceService;
import com.ltt.service.UserInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;

@Service
public class ResourceServiceImpl implements ResourceService {

    @Autowired
    private ResourcesMapper resourcesMapper;
    @Autowired
    private UserInfoService userInfoService;
    @Override
    public HashSet<FileItems> selectListByLike(String content) {
        String name = content;
        String subject = content;
        List<FileItems> file1 = resourcesMapper.selectByFileName(name);
        List<FileItems> files2 = resourcesMapper.selectBySubject(subject);
        List<FileItems> files = resourcesMapper.selectByContent(content);
        List<FileItems> res = new ArrayList<FileItems>();
        if(file1.size() > 0){
            res.addAll(file1);
        }
        if(files2.size() > 0){
            res.addAll(files2);
        }
        if(files.size() > 0){
            res.addAll(files);
        }
        for(FileItems file : res){
            User user = userInfoService.findUserById(file.getUserId());
            file.setUser(user);
        }
        HashSet<FileItems> set = new HashSet<>(res);
        return set;
    }

    @Override
    public HashSet<FileItems> findByContents(String openid, String name, String content, String subject) {
        List<FileItems> res = new ArrayList<FileItems>();
        if(openid.length() > 0){
            List<FileItems> filess = resourcesMapper.selectByOpenId(openid);
            if(filess.size() > 0){
                res.addAll(filess);
            }
        }
        if(name.length() > 0) {
            List<FileItems> file1 = resourcesMapper.selectByFileName(name);
            if (file1.size() > 0) {
                res.addAll(file1);
            }
        }

        if(subject.length() > 0){
            List<FileItems> files2 = resourcesMapper.selectBySubject(subject);
            if(files2.size() > 0){
                res.addAll(files2);
            }
        }
        if(content.length() > 0){
            List<FileItems> files = resourcesMapper.selectByContent(content);
            if(files.size() > 0){
                res.addAll(files);
            }
        }
        if(res.size() == 0){
            List<FileItems> files = resourcesMapper.selectByContent(content);
            if(files.size() > 0){
                res.addAll(files);
            }
        }
        for(FileItems file : res){
            User user = userInfoService.findUserById(file.getUserId());
            file.setUser(user);
        }
        HashSet<FileItems> set = new HashSet<>(res);
        return set;
    }

}
